import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-28
 * Time: 20:47
 */
public class MyStack {
    Queue<Integer> queue1 = null;
    Queue<Integer> queue2 = null;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {

        if (!queue1.isEmpty()) {
            queue1.offer(x);
        }else {
            queue2.offer(x);
        }
    }

    public int pop() {
        if (queue1.isEmpty()&&queue2.isEmpty()) {
            System.out.println("栈为空！");
            return -1;
        }

        if (!queue1.isEmpty()) {
            int num = queue1.size();
            while (num > 1) {
                int tmp = queue1.poll();
                queue2.offer(tmp);
                num--;
            }
            return queue1.poll();
        }else {
            int num = queue2.size();
            while (num > 1) {
                int tmp = queue2.poll();
                queue1.offer(tmp);
                num--;
            }
            return queue2.poll();
        }
    }

    public int top() {
        if (queue1.isEmpty()&&queue2.isEmpty()) {
            System.out.println("栈为空！");
            return -1;
        }

        if (!queue1.isEmpty()) {
            int num = 0;
            while (!queue1.isEmpty()) {
                num = queue1.poll();
                queue2.offer(num);
            }
            return num;
        }else {
            int num = 0;
            while (!queue2.isEmpty()) {
                num = queue2.poll();
                queue1.offer(num);
            }
            return num;
        }
    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}
